<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-11-07 15:26:52
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" 
-->
<template>
	<sc-VxeTable
		ref="xGrid1"
		:gridEvts="gridEvents"
		:tablecode="'storageActionPurchase'"
		:url="this.$API.purchase.storage.list"
		:params="[{ 'type.category_id': 3 }]"
	>
	</sc-VxeTable>

	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		@success="handleSuccess"
		@closed="dialog.save = false"
	>
	</save-dialog>
	<checkhc
		v-if="dialog.checkhc"
		ref="checkhc"
		@success="handleSuccess"
		@closed="dialog.save = false"
	>
	</checkhc>
</template>
<script>
import SaveDialog from "./save";
import checkhc from "./checkhc";
import { http } from "@/utils/request";
import { ElNotification } from "element-plus";
export default {
	name: "storageActionPurchaseColor",
	components: {
		SaveDialog,
		checkhc,
	},
	data() {
		return {
			dialog: {
				save: false,
				checkhc: false,
			},
			gridEvents: {
				cellDblclick: ({ row }) => {
					if (row.instorage_way.type_id == 1) this.checkhc(row);
				},
				checkhc: (param) => {
					let row = param.button.selectRecords[0];
					//入库要检验回潮，出库，直接审核
					if (row.instorage_way.type_id == 1) this.checkhc(row);
					else
						http.post(
							`${this.$API.purchase.storage.list.url}/../audit`,
							{
								ids: param.button.selectRecords.map((el) =>
									el.instorage_way.type_id == 1 ? 0 : el.id
								),
							}
						).then((data) => {
							if (data.code == "200") {
								ElNotification({
									title: "操作成功！",
									message: data.message,
									type: "success",
								});
								this.handleSuccess();
							}
						});
				},
				add: () => {
					this.openSave();
				},
				edit: () => {
					this.edit();
				},
				del: () => {
					this.del();
				},
			},
		};
	},
	methods: {
		openSave: function () {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			});
		},
		handleSuccess() {
			this.$refs.xGrid1.refresh();
		},
		edit(item) {
			let selected = {};
			if (typeof item == "undefined") {
				let selects = this.$refs.xGrid1.xGrid.getCheckboxRecords();
				if (selects.length == 0) {
					this.$message.error("必须选中元素");
					return;
				} else {
					selected = selects[0];
				}
			} else selected = item;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(selected);
			});
		},
		checkhc(item) {
			let selected = {};
			if (typeof item == "undefined") {
				let selects = this.$refs.xGrid1.xGrid.getCheckboxRecords();
				if (selects.length == 0) {
					this.$message.error("必须选中元素");
					return;
				} else {
					selected = selects[0];
				}
			} else selected = item;
			this.dialog.checkhc = true;
			this.$nextTick(() => {
				this.$refs.checkhc.open("edit").setData(selected);
			});
		},
	},
};
</script>
